package com.easylive.admin.controller;

import com.easylive.entity.po.StatisticsInfo;
import com.easylive.entity.query.StatisticsInfoQuery;
import com.easylive.entity.vo.ResponseVO;
import com.easylive.service.StatisticsInfoService;
import com.easylive.utils.DateUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Author: yudaxian
 * @DateTime: 2024/12/27 16:47
 **/
@RestController
@RequestMapping("/index")
public class IndexController extends ABaseController {

    @Resource
    private StatisticsInfoService statisticsInfoService;

    @PostMapping("/getActualTimeStatisticsInfo")
    public ResponseVO getActualTimeStatisticsInfo() {
        // 前一天的时间
        String beforeDate = DateUtil.getBeforeDate(1);

        StatisticsInfoQuery statisticsInfoQuery = new StatisticsInfoQuery();
        statisticsInfoQuery.setStatisticsDate(beforeDate);
        List<StatisticsInfo> list = statisticsInfoService.findListByParam(statisticsInfoQuery);

        Map<Integer, Integer> preDateMap = list.stream().collect(Collectors.toMap(StatisticsInfo::getDataType, StatisticsInfo::getStatisticsCount,
                (item1, item2) -> item2));

        Map<String, Integer> totalCountInfo = statisticsInfoService.getStatisticsInfoActualTime(null);

        Map<String, Object> result = new HashMap<>();
        result.put("preDayData", preDateMap);
        result.put("totalCountInfo", totalCountInfo);
        return getSuccessResponseVO(result);
    }

    @PostMapping("/getWeekStatisticsInfo")
    public ResponseVO getWeekStatisticsInfo(@Validated @NotNull Integer dataType) {
        List<String> beforeDates = DateUtil.getBeforeDates(7);
        StatisticsInfoQuery statisticsInfoQuery = new StatisticsInfoQuery();
        statisticsInfoQuery.setDataType(dataType);
        statisticsInfoQuery.setStatisticsDateStart(beforeDates.get(0));
        statisticsInfoQuery.setStatisticsDateEnd(beforeDates.get(beforeDates.size() - 1));
        statisticsInfoQuery.setOrderBy("statistics_date asc");
        List<StatisticsInfo> list = statisticsInfoService.findListByParam(statisticsInfoQuery);

        Map<String, StatisticsInfo> dataMap = list.stream().collect(Collectors.toMap(StatisticsInfo::getStatisticsDate, Function.identity(), (item1, item2) -> item2));

        List<StatisticsInfo> resultDataList = new ArrayList<>();
        for (String date : beforeDates) {
            StatisticsInfo dataItem = dataMap.get(date);
            if (dataItem == null) {
                dataItem = new StatisticsInfo();
                dataItem.setStatisticsCount(0);
                dataItem.setStatisticsDate(date);
            }
            resultDataList.add(dataItem);
        }

        return getSuccessResponseVO(resultDataList);
    }

}
